package com.yang.system.domain.orgMng.org.validator;

import com.yang.system.domain.common.exception.BusinessException;
import com.yang.system.domain.orgMng.emp.EmpRepository;
import com.yang.system.domain.orgMng.emp.EmpStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class OrgLeaderValidator {
    private final EmpRepository empRepository;
    @Autowired
    public OrgLeaderValidator(EmpRepository empRepository) {
        this.empRepository = empRepository;
    }

    // 组织负责人可以空缺，如果有的话，必须是一个在职员工（含试用期）
    public void leaderShouldBeEffective(Long tenantId, Long superiorId) {
        if (superiorId != null
                && !empRepository.existsByIdAndStatus(tenantId, superiorId
                , EmpStatus.REGULAR, EmpStatus.PROBATION)) {
            throw new BusinessException("组织负责人(id='"
                    + superiorId + "')不是在职员工！");
        }
    }

    public void verify(Long tenantId, Long superiorId) {
        leaderShouldBeEffective(tenantId, superiorId);
    }
}
